$(function () {

	function _operation_field_formatter(cellValue , options , rowData){
		var mode = 'view';
		var buttons = [];
		var id = rowData['id'];
		buttons.push('<div class="grid-operation-button">')

		if(mode == 'view') {
			buttons.push(str_format('<a class="label label-info" target="_blank" href="info.html?id={0}">查看</a>',rowData['id']));
		}
		if(mode == 'modify') {
			buttons.push(str_format('<a class="label label-info" onclick="T.vm.update(' + id + ',event)" href="javascript:;">修改</a>', rowData['id']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

    $("#jqGrid").jqGrid({
        url: baseURL + 'user/userpayment/list',
        datatype: "json",
		postData:requestParameters,
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '订单编号 订单表的ID', name: 'orderId', index: 'order_id', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/orderId')}, 			
			{ label: '用户编号', name: 'uid', index: 'uid', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/uid')},
			{ label: '类型 购买=1,充值=2,退款=-1', name: 'type', index: 'type', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/type')}, 			
			{ label: '发生金额 0', name: 'occurValue', index: 'occur_value', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/occurValue')}, 			
			{ label: '后期余额 0', name: 'postValue', index: 'post_value', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/postValue')}, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/remark')}, 			
			{ label: '支付状态 支付成功=1', name: 'state', index: 'state', width: 80 ,formatter : get_grid_column_formatter('user/userpayment/state')}, 			
			{ label: '添加时间', name: 'adddate', index: 'adddate', width: 80 ,formatter : timestampForMS}
			,{ label: '操作', name: '_operation_field', index: '_operation_field', width:80 ,formatter:_operation_field_formatter}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm  = T.vm = new Vue({
	el:'#webpage',
	data:{
		appearance : {
			showList: true,
			showControllBar : true,
			showSearchForm : false
		},
		q:{
			_keyword:''
		},
		title: null,
		userPayment: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.appearance.showList = false;
			vm.title = "新增";
			vm.userPayment = {};
		},
		update: function (targetId,event) {
			var id = targetId || getSelectedRow();
			if(id == null){
				return ;
			}
			vm.appearance.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.userPayment.id == null ? "user/userpayment/save" : "user/userpayment/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.userPayment),
			    success: function(r){
			    	if(r.code > 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "user/userpayment/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code > 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "user/userpayment/info/"+id, function(r){
                vm.userPayment = r.userPayment;
            });
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'_keyword': vm.q._keyword},
                page:page
            }).trigger("reloadGrid");
		}
	}
});